/**
 * 
 */
package cn.jhz.filesharingsystem.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.jhz.filesharingsystem.model.Pager;
import cn.jhz.filesharingsystem.model.Permission;
import cn.jhz.filesharingsystem.model.SystemContext;
import cn.jhz.filesharingsystem.model.User;
import cn.jhz.filesharingsystem.service.PermissionService;
import cn.jhz.filesharingsystem.util.HeadersUtil;
import cn.jhz.filesharingsystem.web.AuthClass;
import cn.jhz.filesharingsystem.web.AuthMethod;

/**
 * @author asus
 *
 */
@AuthClass
@Controller
public class PermissionController {
	
	@Autowired
	private PermissionService permissionService;
	
	
	
	@AuthMethod
	@RequestMapping(value = "/permission_management", method = RequestMethod.GET)
	private String permissionManager(@RequestHeader(value="User-Agent") String userAgent) {
		if(HeadersUtil.checkAgentIsMobile(userAgent)) {
			return "mobilePermissionManagement";
		}
		return "permissionManagement";
	}
	
	@AuthMethod
	@PostMapping("/permission")
	private @ResponseBody String addPermission(@RequestBody Permission permission, HttpSession session) {
		try {
			User loginUser = (User)session.getAttribute("loginUser");
			if(loginUser.getRole().getState()==0 || loginUser.getState()==0) {
				throw new RuntimeException("帐号已被禁用");
			}
			permissionService.add(permission);
		}catch (Exception e) {
			return "error";
		}
		return "ok";
	}
	
	@AuthMethod
	@DeleteMapping("/permissions")
	public @ResponseBody String deletePermissions(@RequestParam(value="ids[]") int[] ids, HttpSession session) {
		try {
			User loginUser = (User)session.getAttribute("loginUser");
			if(loginUser.getRole().getState()==0 || loginUser.getState()==0) {
				throw new RuntimeException("帐号已被禁用");
			}
			for(int id:ids) {
				permissionService.delete(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "ok";
	}
	
	@AuthMethod
	@PutMapping("/permissions")
	private @ResponseBody String updatePermissions(HttpSession session,@RequestBody Integer...ids){
		try {
			User loginUser = (User)session.getAttribute("loginUser");
			if(loginUser.getRole().getState()==0 || loginUser.getState()==0) {
				throw new RuntimeException("帐号已被禁用");
			}
			for (Integer id : ids) {
				permissionService.updateState(id);
			}
		}catch (Exception e) {
			return "error";
		}
		return "ok";
	}
	
	@AuthMethod
	@GetMapping("/permissions_list")
	private @ResponseBody List<Map<String, String>> getPermissions(HttpSession session){
		List<Permission> permissions = null;
		List<Map<String, String>> permissionList = new ArrayList<>();
		Map<String, String> permissionMap;
		
		User loginUser = (User)session.getAttribute("loginUser");
		if(loginUser.getRole().getState()==0 || loginUser.getState()==0) {
			throw new RuntimeException("帐号已被禁用");
		}
		
		if(((boolean)session.getAttribute("isadmin"))) {
			permissions = permissionService.getAllPermission(false);
		}else if(loginUser.getRole().getRoleName().equals("admin")) {
			permissions = permissionService.getAllPermission(true);
		}
		

		for (Permission permission : permissions) {
			permissionMap = new HashMap<>();
			permissionMap.put("id", String.valueOf(permission.getId()));
			permissionMap.put("text", permission.getResource());
			permissionList.add(permissionMap);
		}
		
		return permissionList;
	}
	
	@AuthMethod            
	@GetMapping("/permissions")
	private @ResponseBody Pager<Permission> getPermissionsPage(@RequestParam(value = "page", required = false)Integer page
			   ,@RequestParam(value = "rows", required = false) Integer rows, HttpSession session) {		
		User loginUser = (User)session.getAttribute("loginUser");
		if(loginUser.getRole().getState()==0 || loginUser.getState()==0) {
			throw new RuntimeException("帐号已被禁用");
		}
		if(page != null && page > 0) SystemContext.setPageOffset((page-1)*rows);
		if(rows != null && rows > 0) SystemContext.setPageSize(rows);
		
		Pager<Permission> pager = permissionService.getAllPagerPermission();
		
		SystemContext.removePageOffset();
		SystemContext.removePageSize();
		
		return pager;
	}
	
	@AuthMethod
	@GetMapping("/home")
	public String main(@RequestHeader(value="User-Agent") String userAgent) {
		if(HeadersUtil.checkAgentIsMobile(userAgent)){
			return "mobileHome";
		}
		return "userHomepage";
	}
	
	
	@AuthMethod
	@GetMapping("/interface")
	public @ResponseBody String getFunctions(HttpSession session) {		
		
		switch (((User)session.getAttribute("loginUser")).getRole().getRoleName()) {
		case "super":
			return "[\"user_management\",\"role_management\",\"permission_management\",\"file_management\"]";
		case "admin":
			return "[\"user_management\",\"role_management\",\"file_management\"]";
		case "vip":
			return "[\"file_management\"]";
		case "guest":
			return "[\"file_management\"]";
		default:
			throw new RuntimeException("该用户已被禁用！");
		}

	}
}
